#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;
using namespace CocosDenshion;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback) );
    pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition( CCPointZero );
    this->addChild(pMenu, 1);

    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    CCLabelTTF* pLabel = CCLabelTTF::create("Steering Behavious", "Thonburi", 15);

    // ask director the window size
    CCSize size = CCDirector::sharedDirector()->getWinSize();

    // position the label on the center of the screen
    pLabel->setPosition( ccp(size.width / 2, size.height - 20) );

    // add the label as a child to this layer
    this->addChild(pLabel, 1);

    radius = 10.0f;
    distance = 100.0f;
    angleNoise = 0.1f;
    speed = 1.0f;
    
    CCSprite *pSprite = CCSprite::create("background.png");
    pSprite->setPosition( ccp(size.width/2, size.height/2) );
    this->addChild(pSprite, -1);
    
    bird = CCSprite::create("bird.png");
    bird->setPosition(ccp(size.width*0.5, size.height*0.5));
    bird->setColor(ccWHITE);
    this->addChild(bird, 0);
    
    this->scheduleUpdate();
    
    return true;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

#pragma mark -Math
float HelloWorld::randfrom(float start, float end)
{
    float randomFloat=(rand()%1000)/1000.0;
    return randomFloat*(end-start)+start;
}

CCPoint HelloWorld::normalizeVector(CCPoint vector)
{
    float length=sqrtf(vector.x*vector.x+vector.y*vector.y);

    if (length<0.000001)
        return ccp(0,1);
    
    return ccpMult(vector, 1/length);
}

float HelloWorld::angleForVector(CCPoint vector)
{
    float alfa = atanf(vector.x/vector.y)*180/3.14;
    
    if (vector.y<0)
        alfa=alfa+180;
    
    return alfa;
}

void HelloWorld::wrapAround(void)
{
    if (bird->getPosition().x>480)
        bird->setPosition(ccp(bird->getPosition().x-480, bird->getPosition().y));
    if (bird->getPosition().x<0)
        bird->setPosition(ccp(bird->getPosition().x+480, bird->getPosition().y));
    if (bird->getPosition().y>320)
        bird->setPosition(ccp(bird->getPosition().x, bird->getPosition().y-320));
    if (bird->getPosition().y<0)
        bird->setPosition(ccp(bird->getPosition().x, bird->getPosition().y+320));
}

void HelloWorld::update(float dt)
{
    // steer toward the target
    CCPoint desiredDirection = this->normalizeVector(ccpSub(targetLoc, bird->getPosition()));
    velocity = ccpMult(desiredDirection, speed);
    bird->setPosition(ccpAdd(bird->getPosition(), velocity));
    bird->setRotation(this->angleForVector(velocity));
    this->wrapAround();
    
    // find the center of the circle
    CCPoint circleLoc = this->normalizeVector(velocity);
    circleLoc = ccpMult(circleLoc, distance);
    circleLoc = ccpAdd(bird->getPosition(), circleLoc);
    
    // find a point on the circle's perimiter for our target
    angle = angle + this->randfrom(-angleNoise, angleNoise);
    CCPoint perimiterPoint = ccp(cosf(angle), sinf(angle));
    perimiterPoint = ccpMult(perimiterPoint, radius);
    targetLoc = ccpAdd(circleLoc, perimiterPoint);
}

void HelloWorld::draw()
{
    ccDrawColor4F(1.0f, 1.0f, 1.0f, 1.0f);
    glLineWidth(1.0f);
    ccDrawCircle(targetLoc, 10, 1, 15, true);
}
